# -*- coding: utf-8 -*-
"""
  @author: xiaoxiong
  @data:  2021/10/18 21:33
  @function:
"""
import pygame
from .. import constants as c
from . import coin
from .. import setup, tools
pygame.font.init()


class Info:
    # state 区分游戏各个季阶段要展示的不同文字信息
    def __init__(self, state, game_info):
        self.state = state
        self.game_info = game_info
        # 创建1阶段特有的文字
        self.create_state_labels()
        # 创建通用的文字信息
        self.create_info_labels()
        # 金币
        self.flash_coin = coin.FlashingCoin()

    def create_state_labels(self):
        self.state_labels = []
        if self.state == "main_menu":
            self.state_labels.append((self.create_labels('1 PLAYER GAME'), (272, 360)))
            self.state_labels.append((self.create_labels('2 PLAYER GAME'), (272, 405)))
            self.state_labels.append((self.create_labels('TOP -'), (290, 465)))
            self.state_labels.append((self.create_labels('000000'), (400, 465)))
        elif self.state == 'load_screen':
            self.state_labels.append((self.create_labels('WORLD'), (280, 200)))
            self.state_labels.append((self.create_labels('1 - 1'), (430, 200)))
            self.state_labels.append((self.create_labels('X   {}'.format(self.game_info['lives'])), (380, 280)))
            # 玩家图片
            self.player_image = tools.get_image(setup.GRAPHICS['mario_bros'], 178, 32, 12, 16, (0, 0, 0), c.BG_MULTI)
        elif self.state == 'game_over':
            self.state_labels.append((self.create_labels('GAME OVER'), (280, 300)))


    def create_info_labels(self):
        self.info_labels = []
        self.info_labels.append((self.create_labels('MARIO'), (75, 30)))
        self.info_labels.append((self.create_labels('WORLD'), (450, 30)))
        self.info_labels.append((self.create_labels('TIME'), (625, 30)))
        self.info_labels.append((self.create_labels('000000'), (75, 55)))
        self.info_labels.append((self.create_labels('x00'), (300, 55)))
        self.info_labels.append((self.create_labels('1 - 1'), (480, 55)))

    '''
    创建文字要调用的公共方法
    label：文字内容
    size：字体大小
    width_scale，height_scale：缩放大小
    返回：把文字渲染成一张图片返回
    '''

    def create_labels(self, label, size=40, width_scale=1.25, height_scale=1):
        # 新建字体 Sys 系统自带字体 这里把字体写成常量放进 constants.py 中
        font = pygame.font.SysFont(c.FONT, size)
        # 文字渲染成图片
        # 参数：文字内容; 是否抗锯齿；文字颜色
        label_image = font.render(label, 1, (255, 255, 255))
        # 产生锯齿效果的图片
        rect = label_image.get_rect()
        label_image = pygame.transform.scale(label_image, (int(rect.width * width_scale),
                                                           int(rect.height * height_scale)))
        return label_image

    # 更新分数时间等信息
    def update(self):
        self.flash_coin.update()

    # 显示更新的信息，传入一个图层，往图层上放文字
    def draw(self, surface):

        # 测试代码
        # surface.blit(self.create_labels('GAME START', size=60), (100, 400))

        for label in self.state_labels:
            surface.blit(label[0], label[1])

        for label in self.info_labels:
            surface.blit(label[0], label[1])

        # 画金币
        surface.blit(self.flash_coin.image, self.flash_coin.rect)

        # 画载入界面的人物
        if self.state == 'load_screen':
            surface.blit(self.player_image, (300, 270))

